package com.dm.cloud.images.ws.callback;

import com.alibaba.fastjson2.JSONObject;
import com.github.dockerjava.api.model.Statistics;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;

import javax.websocket.Session;

/**
 * 容器状态监控数据callback
 */
@Slf4j
public class ContainerMonitorCallback extends AbstractResultCallback<Statistics> {


    public ContainerMonitorCallback(Session session) {
        super("容器状态", session);
    }


    @SneakyThrows
    @Override
    public void onNext(Statistics statistics) {
        sendMessage(JSONObject.toJSONString(statistics));
    }

    @Override
    @SneakyThrows
    public void onError(Throwable throwable) {
//        sendMessage("出现异常:" + throwable.getMessage() + "\n");
        this.close();
    }

    @Override
    @SneakyThrows
    public void onComplete() {
//        sendMessage("服务已关闭，如需再次进入请尝试刷新或者重新进入 \n");
        this.close();
    }

    @Override
    @SneakyThrows
    public void close(){
        if (closeable != null) {
            this.closeable.close();
        }
    }

}
